package com.ruifight.data.common;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BaseController {
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.session = request.getSession();
//		response.setContentType("text/json");
//		response.setCharacterEncoding("GBK");
	}
//	public ArrayList<Object> weixin_fenxiang(String access_token_Msg, String thisUrl){
//		//分享接口
//		String dangqianurl = thisUrl;
//		if(dangqianurl.contains(".jsp")==true){
//			dangqianurl=dangqianurl.replace(".jsp",".html");
//		}else{}
//		if(dangqianurl.contains("/page")==true){
//			dangqianurl=dangqianurl.replace("/page","");
//		}else{}
//
//
//		if(dangqianurl.contains("search.html")==true){
//			dangqianurl=dangqianurl.replace("search.html","search/search.html");
//		}else
//		if(dangqianurl.contains("invite/indexall.html")==true){
//			dangqianurl=dangqianurl.replace("invite/indexall.html","search/indexall.html");
//		} else
//		if(dangqianurl.contains("housekeeping/indexall.html")==true){
//			dangqianurl=dangqianurl.replace("housekeeping/indexall.html","house/index.html");
//		} else
//		if(dangqianurl.contains("edu/eduindex.html")==true){
//			dangqianurl=dangqianurl.replace("edu/eduindex.html","search/eduindex.html");
//		} else
//		if(dangqianurl.contains("idle/indexall.html")==true){
//			dangqianurl=dangqianurl.replace("idle/indexall.html","idle/indexList.html");
//		} else
//		if(dangqianurl.contains("idle/details.html")==true){
//			dangqianurl=dangqianurl.replace("idle/details.html","idle/detail.html");
//		}
//		if(dangqianurl.contains("housekeeping/details.html")==true){
//			dangqianurl=dangqianurl.replace("housekeeping/details.html","house/details.html");
//		} else
//		if(dangqianurl.contains("invite/invitedetails.html")==true){
//			dangqianurl=dangqianurl.replace("invite/invitedetails.html","search/invitedetails.html");
//		}
//		else
//		if(dangqianurl.contains("edu/details.html")==true){
//			dangqianurl=dangqianurl.replace("edu/details.html","search/details.html");
//		}
//		String access_token = access_token_Msg;
//		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
//		url = url.replace("ACCESS_TOKEN", access_token);
//		String jsonticket = HttpUtil.getUrl(url);
//		JSONObject ticketJson = JSONObject.fromObject(jsonticket);
//		String ticket = ticketJson.getString("ticket");
//		Date date = new Date();
//		int timestamp = (int) (date.getTime() / 1000);
//		String signatureString = "jsapi_ticket=" + ticket
//				+ "&noncestr=ruifight2015" + "&timestamp=" + timestamp
//				+ "&url="+dangqianurl;
//		MessageDigest md;
//		List<Object> list=new ArrayList<Object>();
//		try {
//			md = MessageDigest.getInstance("SHA-1");
//			md.reset();
//			md.update(signatureString.getBytes("utf-8"));
//			String signature = byteToHex(md.digest());
//			list.add(0,timestamp);
//			list.add(1,signature);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return (ArrayList<Object>) list;
//	}
//	private String byteToHex(byte[] digest) {
//		Formatter formatter = new Formatter();
//		for (byte b : digest) {
//			formatter.format("%02x", b);
//		}
//		String result = formatter.toString();
//		formatter.close();
//		return result;
//	}
	public void initBinder(WebDataBinder binder, WebRequest request) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(df,
				false));

	}

}
